package com.cpi.cornpp.bushelversion.services;

import java.util.Iterator;
import java.util.List;

import com.cpi.cornpp.bushelversion.converter.BushelsVersionConverter;
import com.cpi.cornpp.bushelversion.dao.BushelsVersionDAO;
import com.cpi.cornpp.bushelversion.dto.BushelsVersionDTO;
import com.cpi.cornpp.bushelversion.exception.BushelsVersionException;
import com.cpi.cornpp.bushelversion.view.BushelsVersionView;
import com.cpi.cornpp.common.exception.CornPPException;
/**
 * Bushels Version Service
 * @author <i>Edgar Joao</i> 
 * @version 1.0
 */
public class BushelsVersionService {
	/**
	 * Bushel Versions List
	 * @author Edgar Joao
	 * @param versionId
	 * @return
	 * @throws BushelsVersionException
	 */
	public List getBushelsVersions(int versionId) throws BushelsVersionException{
		BushelsVersionDAO dao = null;
		List listOfBushelsVersions = null;
		BushelsVersionConverter converter = null;
		try{
			dao = new BushelsVersionDAO();
			converter = new BushelsVersionConverter();
			listOfBushelsVersions = converter.convertDTOsToViews(dao.getBushelsVersions(versionId));			
		} catch (BushelsVersionException bushelsVersionException) {
			throw bushelsVersionException;
		} catch (Exception e){
			BushelsVersionException bushelsVersionException = null;
			e.printStackTrace(System.out);
			bushelsVersionException = new BushelsVersionException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw bushelsVersionException;
		}				
		return listOfBushelsVersions;
	}
	/**
	 * Bushels Version Update
	 * 
	 * @param bushelsVersionsList
	 * @throws BushelsVersionException
	 */
	public void updatePlantsVersions(List bushelsVersionsList) throws BushelsVersionException{		
		Iterator iterator = null;
		BushelsVersionDAO bushelsVersionDAO = null;
		BushelsVersionConverter bushelVersionConverter = null;
		BushelsVersionDTO bushelsVersionDTO = null;
		BushelsVersionView bushelsVersionView = null;		
		try{
			iterator = bushelsVersionsList.iterator();
			bushelsVersionDAO = new BushelsVersionDAO();
			bushelVersionConverter = new BushelsVersionConverter();
			while(iterator.hasNext()){
				bushelsVersionView = (BushelsVersionView) iterator.next();
				bushelsVersionDTO = bushelVersionConverter.converterBushelsVersionViewToDto(bushelsVersionView);
				bushelsVersionDAO.updateBushelsVersions(bushelsVersionDTO);
			}			
		} catch (BushelsVersionException bushelsVersionException) {
			throw bushelsVersionException;
		} catch (Exception e){
			BushelsVersionException bushelsVersionException = null;
			e.printStackTrace(System.out);
			bushelsVersionException = new BushelsVersionException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw bushelsVersionException;
		}	
	}
}
